package logic;

import gamepieces.Armory;
import gamepieces.Cannon;
import gamepieces.Catapult;
import gamepieces.House;
import gamepieces.Interior;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;

import main.GameDef;


public class Manager implements GameDef{
/*
 * Atributes
 */
	public Vector <Player>players;
	public int numPlayers;
	public int currentPlayer;
	public int lastPlayer;
	final static int MAX_PLAYERS = 6;
	final static int NO_GAME_OVER = -1;
	
/*
 * Constructor
 */
	public Manager(){
		numPlayers = MAX_PLAYERS;
		newGame();
	}

	public Manager(int _numPlayers){
		numPlayers = _numPlayers;
		newGame();
	}
	
/*
 * Methods
 */
	public void newGame(){
		players = new Vector<Player>(0);
	}
	
	public boolean addPlayer(String playerName){
		int cantPlayers = players.size();
		players.add(new Player(playerName, cantPlayers));
		
		if(players.size() == numPlayers){
			return true;
		} else {
			return false;
		}
	}
	
	public void startGame(){
	
	}
	
	public int getPieceType(String piece){
		for(int i = 0; i < STRING_PIECES.length; i++){
			if(piece.equals(STRING_PIECES[i]))
				return i;
		}

		return -1;
	}
	
	public void addPiece(String piece){
		Player p = (Player)players.get(currentPlayer);

		switch(getPieceType(piece)){
			case ID_INTERIOR:
				p.castle.add(new Interior());
				break;
			case ID_HOUSE:
				p.castle.add(new House());
				break;
			case ID_CATAPULT:
				p.castle.add(new Catapult());
				break;
			case ID_CANNON:
				p.castle.add(new Cannon());
				break;
			case ID_ARMORY:
				p.castle.add(new Armory());
				break;
		}
	}
	
	public int[] availablePieces(){
		int []playersPieces = new int[players.size()];
		
		for(int i = 0; i < players.size(); i++){
			playersPieces[i] = players.elementAt(i).castle.size();
		}
		
		return playersPieces;
	}
	
	public void updateGold(){
		GameRules.updateGold(players.get(currentPlayer));
	}
	
	public void availableAttacks(){
		GameRules.availableAttacks(players.get(currentPlayer));
	}
	
	public boolean givePiece(int pieceId){
		if(getPieceValue(pieceId) < players.get(currentPlayer).gold){
			return true;
		}
		return false;
	}
	
	public int getPieceValue(int pieceId){
		int value = 0;
		
		switch(pieceId){
			case ID_HOUSE:
				value = HOUSE_VALUE;
				break;
			case ID_CATAPULT:
				value = CATAPULT_VALUE;
				break;
			case ID_CANNON:
				value = CANNON_VALUE;
				break;
			case ID_ARMORY:
				value = ARMORY_VALUE;
				break;
			case ID_INTERIOR:
				value = INTERIOR_VALUE;
				break;
		}
		
		return value; 
	}
	
	public Rectangle createPieceRect(Point position, int pieceId, boolean vertical){
		int x = position.x;
		int y = position.y;
		int w = 0;
		int h = 0;
		
		switch(pieceId){
			case ID_HOUSE:
				w = 10;
				h = 10;
				break;
			case ID_CATAPULT:
				w = 10;
				h = 30;
				break;
			case ID_CANNON:
				w = 20;
				h = 30;
				break;
			case ID_ARMORY:
				w = 30;
				h = 30;
				break;
			case ID_INTERIOR:
				w = 10;
				h = 10;
				break;
		}
		if(vertical){
			return new Rectangle(x, y, w, h);
		} else {
			return new Rectangle(x, y, h, w);
		}
	}
	
	public boolean putPiece(Point position, int pieceId, boolean vertical){
		Rectangle pieceRect = createPieceRect(position, pieceId, vertical);
		for(int i = 0; i < players.get(currentPlayer).castle.size(); i++){
			if(players.get(currentPlayer).castle.get(i).position.intersects(pieceRect) || !players.get(currentPlayer).castlePosition.contains(pieceRect)){
				return false;
			}
		}
		return true;
	}
	
	public int availablePlayers(){
		int []playerPieces = availablePieces();
		int cant = 0;
		
		for(int i = 0; i < playerPieces.length; i++){
			if(playerPieces[i] != 0){
				cant++;
			}
		}
		
		return cant;
	}
	
	public void handleAttacks(Point[] positition, int[] playerId){
		//maneja los ataques a partir de la posicion y id de jugador
	}
	
}
